package com.ruoyi.event.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.dto.EventPlan;
import com.ruoyi.common.core.domain.vo.UserInfoVo;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.service.UserDrugBusinessService;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.UserBusinessSubType;
import com.ruoyi.common.enums.UserDrugType;
import com.ruoyi.common.enums.UserEventType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.event.domain.bo.EventJcjhBo;
import com.ruoyi.event.domain.vo.EventJcjhVo;
import com.ruoyi.event.service.IEventJcjhService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.compress.utils.Lists;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 检测计划
 *
 * @author strongme
 * @date 2023-02-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/event/jcjh")
public class EventJcjhController extends BaseController {

    private final IEventJcjhService iEventJcjhService;
    private final UserDrugBusinessService userDrugBusinessService;

    @GetMapping("generateJcPlanList")
    public R<List<EventPlan>> generateJcPlanList(Long sjskId) {
        UserInfoVo userInfoVo = userDrugBusinessService.queryById(sjskId);
        Date jdsj = userInfoVo.getKeyDate();
        String type = userInfoVo.getType();
        if (jdsj == null || type == null) return R.ok(Lists.newArrayList());
        UserBusinessSubType subType = userInfoVo.getBusinessSubType();
        UserDrugType drugType = UserDrugType.getUserType(type, subType);
        if (drugType == null) return R.ok(Lists.newArrayList());
        List<EventPlan> data = iEventJcjhService.generatePlans(UserEventType.EVENT_NJ, drugType, jdsj, true);
        return R.ok(data);
    }

    /**
     * 查询检测计划列表
     */
    @GetMapping("/list")
    public TableDataInfo<EventJcjhVo> list(EventJcjhBo bo, PageQuery pageQuery) {
        return iEventJcjhService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询检测列表
     *
     * @return
     */
    @PostMapping("query")
    public R<List<EventJcjhVo>> query(@RequestBody EventJcjhBo eventQdBo) {
        return R.ok(iEventJcjhService.queryList(eventQdBo));
    }

    /**
     * 导出检测计划列表
     */
    @Log(title = "检测计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(EventJcjhBo bo, HttpServletResponse response) {
        List<EventJcjhVo> list = iEventJcjhService.queryList(bo);
        ExcelUtil.exportExcel(list, "检测计划", EventJcjhVo.class, response);
    }

    /**
     * 获取检测计划详细信息
     *
     * @param jcjhId 主键
     */
    @GetMapping("/{jcjhId}")
    public R<EventJcjhVo> getInfo(@NotNull(message = "主键不能为空")
                                  @PathVariable Long jcjhId) {
        return R.ok(iEventJcjhService.queryById(jcjhId));
    }

    /**
     * 新增检测计划
     */
    @Log(title = "检测计划", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody EventJcjhBo bo) {
        return toAjax(iEventJcjhService.insertByBo(bo));
    }

    /**
     * 修改检测计划
     */
    @Log(title = "检测计划", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody EventJcjhBo bo) {
        return toAjax(iEventJcjhService.updateByBo(bo));
    }

    /**
     * 删除检测计划
     *
     * @param jcjhIds 主键串
     */
    @Log(title = "检测计划", businessType = BusinessType.DELETE)
    @DeleteMapping("/{jcjhIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] jcjhIds) {
        return toAjax(iEventJcjhService.deleteWithValidByIds(Arrays.asList(jcjhIds), true));
    }
}
